package com.ruoyi.project.configureSystem.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GEOHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.project.configureSystem.domain.StationPicture;
import com.ruoyi.project.configureSystem.domain.VDatainfo;
import com.ruoyi.project.configureSystem.domain.vo.VStationVO;
import com.ruoyi.project.configureSystem.mapper.StationPictureMapper;
import com.ruoyi.project.configureSystem.mapper.SysAreaMapper;
import com.ruoyi.project.configureSystem.mapper.VDatainfoMapper;
import com.ruoyi.project.system.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.project.configureSystem.mapper.VStationMapper;
import com.ruoyi.project.configureSystem.domain.VStation;
import com.ruoyi.project.configureSystem.service.IVStationService;

/**
 * 用户信息Service业务层处理
 * 
 * @author wangshuo
 * @date 2020-05-13
 */
@Service
public class VStationServiceImpl implements IVStationService 
{
    @Autowired
    private VStationMapper vStationMapper;
    @Autowired
    private SysAreaMapper sysAreaMapper;
    @Autowired
    private VDatainfoMapper vDatainfoMapper;
    @Autowired
    private StationPictureMapper stationPictureMapper;
    @Value("${ruoyi.distance}")
    private Double distance;
    /**
     * 查询用户信息
     * 
     * @param id 用户信息ID
     * @return 用户信息
     */
    @Override
    public VStation selectVStationById(Long id)
    {
        VStation vStation = vStationMapper.selectVStationById(id);
        String[] strings = stationPictureMapper.selectStationPictureByStationId(id);
        vStation.setFiles(strings);
        return vStation;
    }

    /**
     * 查询用户信息列表
     * 
     * @param vStation 用户信息
     * @return 用户信息
     */
    @Override
    public List<VStation> selectVStationList(VStation vStation)
    {
        if(vStation.getAreaId()!=null){
            List<Long> areaIds = vStationMapper.selectChildrenByAreaId(vStation.getAreaId());
            vStation.setAreaIds(areaIds);
            vStation.setAreaId(null);
        }
        return vStationMapper.selectVStationList(vStation);
    }

    /**
     * 新增用户信息
     * 
     * @param vStation 用户信息
     * @return 结果
     */
    @Override
    public int insertVStation(VStation vStation)
    {
        vStation.setCreateTime(DateUtils.getNowDate());
        int i = vStationMapper.insertVStation(vStation);
        if(i>0){
            if(vStation.getFiles().length>0){
                List<StationPicture> stationPictures = new ArrayList<>();
                Arrays.stream(vStation.getFiles()).forEach(x->{
                    StationPicture stationPicture = new StationPicture();
                    stationPicture.setStationId(vStation.getId());
                    stationPicture.setUploadBy(SecurityUtils.getLoginUser().getUser().getUserId());
                    stationPicture.setGmtUpload(new Date());
                    stationPicture.setUrl(x);
                    stationPictures.add(stationPicture);
                });
                stationPictureMapper.batchInsert(stationPictures);
            }
        }
        return i;
    }

    /**
     * 修改用户信息
     * 
     * @param vStation 用户信息
     * @return 结果
     */
    @Override
    public int updateVStation(VStation vStation)
    {
        if(vStation.getFiles().length>0) {
            List<StationPicture> stationPictures = new ArrayList<>();
            Arrays.stream(vStation.getFiles()).forEach(x -> {
                StationPicture stationPicture = new StationPicture();
                stationPicture.setStationId(vStation.getId());
                stationPicture.setUploadBy(SecurityUtils.getLoginUser().getUser().getUserId());
                stationPicture.setGmtUpload(new Date());
                stationPicture.setUrl(x);
                stationPictures.add(stationPicture);
            });
            stationPictureMapper.batchInsert(stationPictures);
        }
        return vStationMapper.updateVStation(vStation);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteVStationByIds(Long[] ids)
    {
        Arrays.stream(ids).forEach(x->{
            //判断是否下级存在指标
            VDatainfo vDatainfo = new VDatainfo();
            vDatainfo.setVstationId(x);
            List<VDatainfo> vDatainfos = vDatainfoMapper.selectVDatainfoList(vDatainfo);
            if(vDatainfos.size()>0){
                throw new BaseException("存在站点，不能删除");
            }
            VStation vStation = new VStation();
            vStation.setId(x);
            vStation.setDelFlag("2");
             vStationMapper.updateVStation(vStation);
        });
//        return vStationMapper.deleteVStationByIds(ids);
        return 1;
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteVStationById(Long id)
    {
        return vStationMapper.deleteVStationById(id);
    }

    @Override
    public List<VStation> getStationByRole(VStation vStation) {
        if(!SecurityUtils.getLoginUser().getUser().isAdmin()){
            List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
            List<Long> collect = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
            vStation.setRoleIds(collect);
        }
        List<VStation> stationByRole = vStationMapper.getStationByRole(vStation);
        stationByRole.forEach(x->{
            String[] files = stationPictureMapper.selectStationPictureByStationId(x.getId());
            x.setFiles(files);
        });
        return stationByRole;
    }

    @Override
    public int delStationImg(String url, Long stationId) {
        int i = stationPictureMapper.delStationImg(url,stationId);
        return i;
    }

    @Override
    public int updateVStationByID(VStation vStation) {
        return vStationMapper.updateVStationByID(vStation);
    }

    @Override
    public List<VStationVO> selectStationByLocation(String longitude, String latitude) {
        List<VStationVO> stationVOS =vStationMapper.selectStationByLocation();
        for (Iterator<VStationVO> iterator = stationVOS.iterator(); iterator.hasNext(); ) {
            VStationVO next = iterator.next();
            //计算经纬度
            double d = GEOHelper.distance(Double.parseDouble(longitude), Double.parseDouble(latitude), Double.parseDouble(next.getLng()), Double.parseDouble(next.getLat()));
            if(distance<d){
                iterator.remove();
            }
            next.setDistance(d);
        }
        return stationVOS.stream().sorted(Comparator.comparing(VStationVO::getDistance).reversed()).collect(Collectors.toList());
    }
}
